// pages/IsUseContribution/index.js

var app = getApp();
var util = require("../../utils/util.js");
var hostUrl = require("../../host.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttonText:"查看计算结果",
    content_view_butm:"none",
    avatarUrl: '',
    nickName: '',
    background_color: "#32CD32",
    is_contribution_valuation:0,
    nextPage: "/pages/pie/index",
    have_calculation :0,
    text:"",
  },

  switchChange: function (e) {

    if (e.detail.value){
     
      this.setData({
        is_contribution_valuation: 1,
        content_view_butm:"",
        buttonText:"下一步",
        nextPage: "/pages/shareDistribution/index",
        background_color:"#288CCB"
      })
    }else{
      this.setData({
        is_contribution_valuation: 0,
        buttonText: "查看计算结果",
        content_view_butm: "none",
        nextPage: "/pages/pie/index",
        background_color:"#32CD32",
      })
      
    }
    // console.log('switch2 发生 change 事件，携带值为', e.detail.value)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('userInfo')) {
      this.setData({
        avatarUrl: JSON.parse(wx.getStorageSync('userInfo')).avatarUrl,
        nickName: JSON.parse(wx.getStorageSync('userInfo')).nickName,
      })
    }
    this.setData({
      have_calculation : app.globalData.have_calculation
    })
    if (app.globalData.have_calculation == 1) {
      this.setData({
        text: "报告记录"
      })
    } else {
      this.setData({
        text: "返回首页"
      })
    }
    this.$wuxToptips = app.Wux().$wuxToptips

  },

  submitForm:function(e){
    var that = this;
    if (e.detail.value.company_valuation){
      app.globalData.entity.company_valuation = e.detail.value.company_valuation;
    }

    var is_contribution_valuation = this.data.is_contribution_valuation;
    app.globalData.entity.is_contribution_valuation = is_contribution_valuation
    
    if (is_contribution_valuation == 0 ){
   
      var url = hostUrl.add_calculator;
      var paramMd5 = app.paramsMd5(app.globalData.entity);
      console.log(paramMd5);
      wx.request({
        url: url,
        data: paramMd5,
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        dataType: 'json',
        success: function (response) {
          if (response.data.status){
            wx.reLaunch({
              url: that.data.nextPage + "?record_id=" + response.data.msg,
            });
          }else{
            var hideToptips = this.$wuxToptips.show({
              timer: 3000,
              text: response.data.msg,
              success: () => console.log('toptips', response.data.msg)
            });
          }
        }
      });
    }else{

      //form表单
      var detailValue = e.detail.value;
      //验证空值
      var rules = {
        company_valuation: {
          required: true,
        }
      };
      var messages = {
        company_valuation: {
          required: "公司估值必填",
        }
      };
      this.WxValidate = app.WxValidate(rules, messages);
      //判断字段空值
      if (!this.WxValidate.checkForm(e)) {
        const error = this.WxValidate.errorList[0]
        this.showToptips(error)
        return false
      }
      
      this.clearGlobalData();
      wx.navigateTo({
        url: that.data.nextPage,
      })
    }
  },
  

  navigateBack:function(e){
    app.globalData.currentBoss = Number(app.globalData.currentBoss) - 1;
    wx.redirectTo({
      url: '/pages/gongxianpingfen/index?isBack=true',
    });
  },

  showToptips(error) {
    const hideToptips = this.$wuxToptips.show({
      timer: 3000,
      text: error.msg || '请填写正确的字段',
      success: () => console.log('toptips', error)
    })
  },

  clearGlobalData:function(){
    app.globalData.shareDistribution = 0;
    app.globalData.shareholders_contribution_valuation_list = {};
  },

  icon_redirectTo: function () {
    var that = this;
    if (that.data.have_calculation == 1) {
      wx.redirectTo({
        url: '/pages/history/index',
      })
    } else {
      wx.redirectTo({
        url: '/pages/index/index',
      });
    }
  },

})